<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexRoleRequest;
use App\Http\Requests\StoreRoleRequest;
use App\Http\Requests\UpdateRoleRequest;
use App\Http\Resources\RoleResource;
use App\Models\Role;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;

class RoleController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    /**
     * Display a listing of the resource.
     */
    public function index(IndexRoleRequest $request, Role $role)
    {
        $this->authorize('viewAny', $role);
        $name = $request->input('name');
        $data = $role->with('statuslabel')
            ->where('name', 'like', "%$name%")->orderBy('order', 'ASC')->paginate(
                $request->query('pageSize'),
                ['*'],
                'page',
                $request->query('pageNum')
            );
        $response = RoleResource::collection($data);
        $total = $response->total(); // 获取分页信息中的总数
        return $this->success('查询成功', $response, $total);
    }

    /**
     * 返回权限菜单ID
     *
     * @param Request $request
     * @param Role $role
     * @param [type] $id
     * @return 
     */
    public function menusId(Request $request, Role $role, $id)
    {
        $role = $role->where('id', $id)->firstOrFail();
        $this->authorize('viewAny', $role);
        $data = $role->menus->pluck('id');
        return $this->success('查询成功', $data);
    }

    public function menus(Request $request, Role $role, $id)
    {
        // 使用 firstOrFail 确保找到对应 ID 的角色，否则抛出异常
        $role = $role->where('id', $id)->firstOrFail();
        $this->authorize('viewAny', $role);
        $data = $request->input();
        // 使用 sync 方法将菜单 id 数组关联到角色 sync(覆盖作用)
        $role->menus()->sync($data);
        return $this->success('关联成功', $role);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreRoleRequest $request, Role $role)
    {
        $this->authorize('create', $role);
        $role->fill($request->input());
        $role->save();
        return $this->success('新增成功', $role);
    }

    /**
     * Display the specified resource.
     */
    public function show(role $role)
    {
        $this->authorize('viewAny', $role);
        return $this->success('查询成功', new RoleResource($role));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(role $role)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateRoleRequest $request, role $role)
    {
        //
        $this->authorize('update', $role);
        $role->fill($request->input());
        $role->save();
        return $this->success('编辑成功', $role);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Role $role)
    {
        $this->authorize('delete', $role);
        $role->delete();
        return $this->success('删除成功', $role);
    }
}
